// @ts-check
import { join } from "path";
import { defineConfig, ConfigEnv, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import UnoCSS from "unocss/vite";
import AutoImport from "unplugin-auto-import/vite";
import VueJsx from "@vitejs/plugin-vue-jsx";
import Components from "unplugin-vue-components/vite";
import NutUIResolver from "@nutui/nutui/dist/resolver";

// https://vitejs.dev/config/
export default ({ command, mode }: ConfigEnv) => {
  const env = loadEnv(mode, process.cwd());

  return defineConfig({
    base: env.VITE_BASE_PATH,
    server: {
      proxy: {
        "/app-api": {
          target: "https://saas.hsjiafu.com/prod/app-api",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/app\-api/, ""),
        },
      },
    },
    plugins: [
      vue(),
      VueJsx(),
      UnoCSS(),
      AutoImport({
        imports: ["vue"],
        dts: "src/types/auto-imports.d.ts",
      }),
      Components({
        resolvers: [NutUIResolver()],
        dts: "src/types/components.d.ts",
      }),
    ],
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@import "@nutui/nutui/dist/styles/variables.scss";`,
        },
      },
    },
    resolve: {
      alias: {
        "@": join(__dirname, "src"),
      },
    },
  });
};
